package com.example.goodtrash.mine;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.example.goodtrash.R;
import com.example.goodtrash.db.RecycleDB;
import com.example.goodtrash.model.Recycle;

import java.util.List;

public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.ViewHolder>{
    private List<Recycle> mRecycleList;
    private Context mcontext;
    private Activity mactivity;
    static class ViewHolder extends RecyclerView.ViewHolder{
        View recycleView;
        TextView tvName;
        TextView tvType;

        TextView tvDataTime;

        public ViewHolder(View view){
            super(view);
            recycleView=view;
            tvName=(TextView) view.findViewById(R.id.tv_name);
            tvType=(TextView) view.findViewById(R.id.tv_type);
            tvDataTime=(TextView) view.findViewById(R.id.tv_datetime);
        }
    }


    public RecycleAdapter(List<Recycle> recycleList,Context context,Activity activity){
        mRecycleList=recycleList;
        mcontext=context;
        mactivity=activity;
    }


    @NonNull
    @Override
    public RecycleAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycle_history,parent,false);
        RecycleAdapter.ViewHolder holder=new RecycleAdapter.ViewHolder(view);
        holder.recycleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position=holder.getAdapterPosition();
                Recycle recycle=mRecycleList.get(position);
                AlertDialog alertDialog = new AlertDialog.Builder(mcontext).setTitle("删除记录")
                        .setMessage("是否确定此删除记录")
                        .setNeutralButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                RecycleDB db=RecycleDB.getInstance(mcontext);
                                db.removeRecycle(recycle);
                                mcontext.startActivity(new Intent(mcontext, recycleHistory.class));
                                mactivity.finish();
                            }
                        })
                        .setPositiveButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        }).create();
                alertDialog.show();
            }
        });
        return holder;
    }


    @Override
    public void onBindViewHolder(@NonNull RecycleAdapter.ViewHolder holder, int position) {
        Recycle recycle=mRecycleList.get(position);
        holder.tvName.setText(recycle.getName());
        holder.tvType.setText(recycle.getType());
        holder.tvDataTime.setText(recycle.getDataTime());

    }

    @Override
    public int getItemCount() {
        return mRecycleList.size();
    }
}
